package com.beiding.workordersystem.orders.looporder.controller;

import com.beiding.workordersystem.common.dao.MPage;
import com.beiding.workordersystem.common.standard.R;
import com.beiding.workordersystem.common.utils.MyBeanUtils;
import com.beiding.workordersystem.orders.looporder.entity.Company;
import com.beiding.workordersystem.orders.looporder.entity.CustomCompanyItem;
import com.beiding.workordersystem.orders.looporder.query.CompanyQuery;
import com.beiding.workordersystem.orders.looporder.service.CompanyService;
import com.beiding.workordersystem.orders.looporder.vo.CompanyVo;
import com.beiding.workordersystem.orders.looporder.vo.CustomCompanyItemVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    @GetMapping("/page")
    R page(MPage page, CompanyQuery company) {
        return R.ok(companyService.page(page.toIPage(), company));
    }

    @GetMapping("/details")
    R details(@RequestParam String id) {
        return R.ok(companyService.details(id));
    }

    @PostMapping("/delete")
    R delete(@RequestBody String[] ids) {
        companyService.delete(ids);
        return R.ok();
    }

    @PostMapping("/workOff")
    R workOff(@RequestBody String[] ids) {
        companyService.workOff(ids);
        return R.ok();
    }

    @PostMapping("/save")
    R save(@RequestBody @Validated CompanyVo vo) {
        Company company = new Company();
        BeanUtils.copyProperties(vo, company);
        companyService.save(company);
        return R.ok(company.getId());
    }

    @PostMapping("/settings/custom/save")
    R settingsSaveCustom(@RequestBody @Validated CustomCompanyItemVo vo) {
        CustomCompanyItem item = MyBeanUtils.copy(vo, CustomCompanyItem.class);
        companyService.saveCustom(item);
        return R.ok(item.getId());
    }

    @PostMapping("/settings/custom/delete")
    R settingsCustomDelete(@RequestBody String[] list) {
        companyService.deleteCustom(list);
        return R.ok();
    }

    @GetMapping("/settings/custom/list")
    R settingsCustomList(){
        return R.ok(companyService.listCustom());
    }

}
